/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.ban.data.exchange.runner.tokenBucket.server;

import cn.ac.iie.ban.data.exchange.runner.tokenBucket.exception.DEFCInitException;
import cn.ac.iie.ban.data.exchange.runner.tokenBucket.exception.DEFCRuntimeEnvException;
import cn.ac.iie.ban.data.exchange.runner.tokenBucket.util.PairList;
import cn.ac.iie.ban.data.exchange.runner.tokenBucket.util.RuntimeEnv;
import cn.ac.iie.ban.data.exchange.runner.tokenBucket.util.ServiceInfo;
import cn.ac.iie.di.commons.httpserver.framework.server.HttpServer;
import java.net.UnknownHostException;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author zwlin
 */
public class HttpService {

    static Logger logger = Logger.getLogger(HttpServer.class.getName());
    HttpServer server;

    public HttpService(String bindIp, int bindPort, int parallelSize) throws Exception {
        server = HttpServer.getHttpServer(bindIp, bindPort, parallelSize);
        server.registerContext("/getToken");
        server.registerContextHandler("/getToken", "/", GetTokenHandler.class);
        server.registerContext("/hello");
        server.registerContextHandler("/hello", "/", HelloHandler.class);
    }

    public HttpService() throws UnknownHostException, DEFCRuntimeEnvException, Exception {
        this(RuntimeEnv.getParamAsString(RuntimeEnv.SERVER_IP), RuntimeEnv.getParamAsInteger(RuntimeEnv.SERVER_PORT), RuntimeEnv.getParamAsInteger(RuntimeEnv.SERVER_PARALLEL_SIZE));
        logger.info("bind ip: " + RuntimeEnv.getParamAsString(RuntimeEnv.SERVER_IP));
    }

    public void initSpeedLimitHelper(ServiceInfo metaService, long updateIntervalSec,
            int totalSpeed,
            PairList<Integer, Integer> speedList,
            PairList<Integer, Integer> dataSizeLimit) throws DEFCInitException {
        GetTokenHandler.initSpeedLimitHelper(metaService, updateIntervalSec, totalSpeed, speedList, dataSizeLimit);
    }

    public void initSpeedLimitHelper(ServiceInfo metaService, long updateIntervalSec,
            int totalSpeed,
            PairList<Integer, Integer> speedList,
            PairList<Integer, Integer> dataSizeLimit,
            Map<String, Integer> specificLimitMap) throws DEFCInitException {
        GetTokenHandler.initSpeedLimitHelper(metaService, updateIntervalSec, totalSpeed, speedList, dataSizeLimit, specificLimitMap);
    }

    public void initStatistic(String rmqNamesrv, String rmqTopic, int intevalSec) throws Exception {
        GetTokenHandler.initStatistic(rmqNamesrv, rmqTopic, intevalSec);
    }

    public void start() throws Exception {
        server.startup();
    }

    public void stopGracefully() throws Exception {
        if (server == null) {
            return;
        }
        GetTokenHandler.stop();
        shutdown();
    }

    public void shutdown() throws Exception {
        if (server == null) {
            return;
        }
        server.shutdown();
    }

}
